//
//  FYModelViewController.swift
//  Mircore
//
//  Created by apple on 16/8/8.
//  Copyright © 2016年 飞羽. All rights reserved.
//

import UIKit

class FYModelViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
 
    
      
        //添加子控制器的两种方式
        
        /**
         * 
         .  viewControllers
         
         
         .addChildViewController(xx)
         */
        
        //全局设置UITabbar的tintColor  执行的越早越好 一般会放到appdelegate里面
        
        UITabBar.appearance().tintColor  = UIColor.orangeColor()
        
        //设置自定义的TabBar
        
        let  fyTabbar  = FYTabBar()
        
        //设置代理对象
        
        fyTabbar.fyDelegate  = self
        
        fyTabbar.composeButtonClosure  = { [weak  self]  in
        
         //进入发微博界面
            
        
           self?.pushComposeVC()
        }
        
         
        
        //使用kvc方式给系统的只读属性设置值
        
        setValue(fyTabbar, forKey: "tabBar")
        
        
        //添加子视图控制器
        
        addChildViewController(FYHomeViewController(), imageName: "tabbar_home", title: "首页")
        
        addChildViewController(FYMessageViewController(), imageName: "tabbar_message_center", title: "消息")
        
        addChildViewController(FYDiscoverViewController(), imageName: "tabbar_discover", title: "发现")
        
        addChildViewController(FYProfileViewController(), imageName: "tabbar_profile", title: "我的")
      
    }

    //重新添加子控制器的函数
    
    func  addChildViewController(childController:UIViewController, imageName: String, title: String)  {
        //设置图片
        
        childController.tabBarItem.image  = UIImage(named: imageName)
        
        //修改渲染模式
        
        childController.tabBarItem.selectedImage  = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //设置标题
        
        childController.title  = title
        
        
        //设置文字图片
        
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        
        
        //设置字体大小
        
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14)], forState: .Normal)
        
        //添加到导航控制器
        
        let  nav = UINavigationController(rootViewController: childController)
        
        //给当前对象添加子视图控制器
        addChildViewController(nav)
    }
    
    
    //进入发微博界面
    
    private  func  pushComposeVC() {
    
      let  composeVC  = FYComposeViewController()
        
        let  nav  =  UINavigationController(rootViewController: composeVC)
        
        
        presentViewController(nav, animated: true, completion: nil)
    
    
    }

 }

extension  FYModelViewController :FYTabBarDelegate  {

  //实现代理方法
    
    func didSelectedComposeButton() {
        
        print("我是代理对象调用过来的")
    }

}
